#include "project_config.h"

#include "xscugic.h"

#if (HAVE_FREERTOS == 1)
#include "qelib.h"
#include "FreeRTOS.h"

QELOG_DOMAIN("port-interrupt");

extern qe_u64 ullPortInterruptNesting;

qe_ret qe_irq_register(qe_int irq, qe_irq_handler handler, void *args)
{
    BaseType_t ret;
    ret = xPortInstallInterruptHandler(irq, (XInterruptHandler)handler, args);
    if (ret != pdPASS) {
        qe_error("install irq:%d err:%d", irq, ret);
        return qe_err_common;
    }
    return qe_ok;
}

void qe_irq_unregister(qe_int irq)
{
    (void)irq;
}

qe_ret qe_irq_enable(qe_int irq)
{
    vPortEnableInterrupt(irq);
    return qe_ok;
}

void qe_irq_disable(qe_int irq)
{
    vPortDisableInterrupt(irq);
}

qe_bool qe_is_interrupt_context(void)
{
    return (ullPortInterruptNesting != 0);
}
#else
#include "qelib.h"
#include "xil_exception.h"
void qe_intc_mount(qe_irq_handler handler, void *args)
{
	Xil_ExceptionInit();
	Xil_ExceptionRegisterHandler(
		XIL_EXCEPTION_ID_INT,
		(Xil_ExceptionHandler)handler,
		args);
	Xil_ExceptionEnable();
}
#endif

extern XScuGic xInterruptController;

void qe_irq_set_priority_trigger_type(qe_u32 irq, qe_u8 prior, qe_u8 trigger)
{
    XScuGic_SetPriorityTriggerType(&xInterruptController, irq, prior, trigger);
}
